<Project>
  <Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.targets, $(MSBuildThisFileDirectory)..))" />
  <PropertyGroup>
    <!--
      If we are stabilizing set the StableVersion property for the packages.
      Needs to happen in Directory.Build.targets to allow all the pkgproj's to set Version property first.
    -->
    <StableVersion Condition="'$(IncludePreReleaseLabelInPackageVersion)' != 'true'">$(Version)</StableVersion>

    <!--
      Explicitly re-set the symbol package output path. The pkgproj files here end up importing the targets from
      Microsoft.DotNet.Build.Tasks.Packaging (based on a PackageReference) before importing the Arcade targets hat
      set defaults for project output paths. This means the value set by the packaging targets does not accountfor
      the updated defaults from the Arcade targets.
    -->
    <SymbolPackageOutputPath>$(PackageOutputPath)</SymbolPackageOutputPath>
  </PropertyGroup>

  <Target Name="SetTargetBasedPackageVersion"
          BeforeTargets="GenerateNuSpec"
          DependsOnTargets="GetProductVersions">
    <PropertyGroup>
      <Version>$(ProductVersion)</Version>
      <!--
        PackageVersion is normally calculated using Version during static property evaluation, but
        we need some info from GetProductVersions, so it's too late to rely on that. We need to set
        both in target evaluation, here.
      -->
      <PackageVersion>$(ProductVersion)</PackageVersion>
    </PropertyGroup>
  </Target>

  <!--
    Remove duplicate files returned by restore. The resolve task performs extra detection to pick up
    a PDB file for any file listed in the assets file. This causes duplicates if the assets file
    lists PDBs. If foo.dll and foo.pdb exist in the package and both are listed in the assets file,
    the task finds:

      foo.dll
      foo.pdb (based on foo.dll entry)
      foo.pdb
      foo.pdb (based on foo.pdb entry)

    The duplicates cause package validation failure and must be filtered out.
  -->
  <Target Name="RemoveDuplicateResolvedNuGetPackageAssets">
    <RemoveDuplicates Inputs="@(ReferenceCopyLocalPaths)">
      <Output TaskParameter="Filtered" ItemName="FilteredReferenceCopyLocalPaths" />
    </RemoveDuplicates>

    <ItemGroup>
      <ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)" />
      <ReferenceCopyLocalPaths Include="@(FilteredReferenceCopyLocalPaths)" />
    </ItemGroup>
  </Target>

  <!--
    Ensures package reports dir exists, to work around the issue in VerifyClosure task:
    https://github.com/dotnet/arcade/issues/6090
    Remove this target, once the issue is fixed.
  -->
  <Target Name="EnsureDepenedencyGraphDir" BeforeTargets="VerifyClosure">
    <MakeDir Directories="$(PackageReportDir)" />
  </Target>

  <!--
    Finds symbol files and injects them into the package build.
  -->
  <Target Name="GetSymbolPackageFiles" BeforeTargets="GetPackageFiles">
    <ItemGroup Condition="'$(SymbolsSuffix)' != ''">
      <AdditionalLibPackageExcludes Include="%2A%2A\%2A$(SymbolsSuffix)"/>
    </ItemGroup>

    <ItemGroup>
      <NativeWithSymbolFile Include="@(NativeBinary)" Condition="'$(PackageTargetRuntime)'!=''">
        <TargetPath>runtimes/$(PackageTargetRuntime)/native</TargetPath>
      </NativeWithSymbolFile>
      <NativeWithSymbolFile Include="@(ArchitectureSpecificToolFile)">
        <TargetPath>tools</TargetPath>
      </NativeWithSymbolFile>
    </ItemGroup>

    <ItemGroup>
      <!-- On Windows, trim ".dll" before adding ".pdb". -->
      <WindowsNativeFile Include="@(NativeWithSymbolFile)"
                         Condition="'%(NativeWithSymbolFile.Extension)'=='.dll' OR '%(NativeWithSymbolFile.Extension)'=='.exe'" />
      <WindowsSymbolFile Include="@(WindowsNativeFile -> '%(RootDir)%(Directory)PDB\%(Filename).pdb')" />
      <!-- Crossgened files (on windows) have both a *.pdb and a *.ni.pdb symbol file.  Include the *.ni.pdb file as well if it exists. -->
      <WindowsSymbolFile Include="@(WindowsNativeFile -> '%(RootDir)%(Directory)PDB\%(Filename).ni.pdb')" />

      <!--
        Search for all xplat symbol file extensions on every xplat native binary. Some binaries have
        no ".so" or ".dylib" extension, so we can't tell which convention its symbol files would
        use. On xplat, the symbol extension is simply appended.
      -->
      <NonWindowsNativeFile Include="@(NativeWithSymbolFile)"
                            Exclude="@(WindowsNativeFile)" />

      <NonWindowsSymbolFile Include="@(NonWindowsNativeFile -> '%(Identity)$(SymbolsSuffix)')" />

      <ExistingWindowsSymbolFile Include="@(WindowsSymbolFile)" Condition="Exists('%(Identity)')" />
      <ExistingNonWindowsSymbolFile Include="@(NonWindowsSymbolFile)" Condition="Exists('%(Identity)') AND '$(SkipPackagingXplatSymbols)'!='true'" />

      <!-- Include all found symbols. -->
      <File Include="@(ExistingWindowsSymbolFile);@(ExistingNonWindowsSymbolFile)">
        <IsSymbolFile>true</IsSymbolFile>
      </File>
    </ItemGroup>

    <PropertyGroup>
      <NeedsPlaceholderPdb Condition="'@(ExistingNonWindowsSymbolFile)'!='' AND '@(ExistingWindowsSymbolFile)'==''">true</NeedsPlaceholderPdb>
    </PropertyGroup>

    <ItemGroup>
      <File Include="$(InstallerProjectRoot)\pkg\_.pdb"
            Condition="'$(NeedsPlaceholderPdb)'=='true' AND '$(PackageTargetRuntime)'!=''">
        <TargetPath>runtimes/$(PackageTargetRuntime)/native</TargetPath>
        <IsSymbolFile>true</IsSymbolFile>
      </File>
    </ItemGroup>
  </Target>

  <!-- override GetPackageIdentity so that it doesn't attempt to gather
       files from runtime dependencies,  this prevents folks from needing
       packages that don't apply to their local OS.
       https://github.com/dotnet/buildtools/issues/1273 -->
  <Target Name="GetPackageIdentity" Returns="@(_PackageIdentity)" DependsOnTargets="CalculatePackageVersion">
    <ItemGroup>
      <_PackageIdentity Include="$(Id)">
        <Version>$(PackageVersion)</Version>
        <TargetRuntime Condition="'$(PackageTargetRuntime)' != ''">$(PackageTargetRuntime)</TargetRuntime>
      </_PackageIdentity>
    </ItemGroup>
  </Target>

  <!--
    Create RuntimeDependencies just before they're needed. Use MSBuild to get their identities, like
    GetPkgProjPackageDependencies: this finds package id and version without any guesswork.
  -->
  <Target Name="CreateRuntimeDependencyItems"
          BeforeTargets="DetermineRuntimeDependencies"
          Condition="'$(PackageTargetRuntime)' == '' and '$(HasRuntimePackages)' != 'false'">
    <MSBuild
      Targets="GetPackageIdentity"
      BuildInParallel="$(BuildInParallel)"
      Projects="@(RuntimeProject -> WithMetadataValue('Extension', '.pkgproj'))">
      <Output TaskParameter="TargetOutputs" ItemName="_runtimeDependencyIdentity" />
    </MSBuild>

    <ItemGroup>
      <RuntimeDependency Include="@(_runtimeDependencyIdentity)" />
    </ItemGroup>
  </Target>
</Project>
